<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('信息记录')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
    <!-- <style type="text/css">.table {table-layout:fixed;}</style> -->
    <style type="text/css">
        .la {
            text-align: left;
        }

        .textBox {
            text-align: right;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="inforecordForm" th:action="@{/inforecord/list}" class="form-inline">
                <!-- <input type="hidden" name="deptId" id="deptId"/> -->
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3">
                            <label>物料号: </label>
                            <input type="text" name="materialCode"/>
                        </li>
                        <li class="col-sm-3">
                            <label>物料组：</label>
                            <input type="text" name="materialGroup"/>
                        </li>
                        <li class="col-sm-3">
                            <label>供应商编码：</label>
                            <input type="text" name="srmCode"/>
                        </li>
                        <li class="col-sm-3">
                            <div>
                                <label>工厂：</label>
                                <input type="text" name="werks"/>
                            </div>
                        </li>

                        <li class="col-sm-3">
                            <div>
                                <label>采购组：</label>
                                <select name="purchaseGroup" sysDict="CGZ" emptyFirst="true"></select>
                            </div>
                        </li>

                        <li class="col-sm-3">
                            <div>
                                <label>确认状态：</label>
                                <select name="infoStatus" emptyFirst="true">
                                    <option></option>
                                    <option value="1">已确认</option>
                                    <option value="0">未确认</option>
                                </select>
                            </div>
                        </li>
                        <li class="col-sm-3">
                            <div>
                                <label>冻结状态：</label>
                                <select name="freeze" emptyFirst="true">
                                    <option></option>
                                    <option value="1">已冻结</option>
                                    <option value="0">未冻结</option>
                                </select>
                            </div>
                        </li>

                        <li class="col-sm-3">
                            <button id="inforecordSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i
                                    class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>
                            <button id="inforecordAdvancedSearch" type="button"
                                    class="btn btn-primary btn-rounded btn-sm" style="display: none"><i
                                    class="fa fa-caret-down"></i>&nbsp;高级搜索
                            </button>
                        </li>
                    </ul>

                </div>
            </form>
        </div>
        <div class="col-sm-12 select-info">
            <div class="btn-group hidden-xs toolbar" id="inforecordToolBar" role="group">
                <a class="btn btn-outline btn-success btn-rounded" onclick="toAddinfo()"
                   shiro:hasPermission="infoRecord:add">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="toEditquotaInfo()"
                   shiro:hasPermission="infoRecord:edit">
                    <i class="fa fa-edit"></i> 编辑
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="confirmInfo()"
                   shiro:hasPermission="infoRecord:confirmInfo">
                    <i class="fa fa-edit"></i> 确认
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="freezeOrThaw()"
                   shiro:hasPermission="infoRecord:freeze">
                    解冻/冻结
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="unconfirmInfo()"
                   shiro:hasPermission="infoRecord:unconfirmInfo">
                    <i class="fa fa-edit"></i> 取消确认
                </a>
                <a class="btn btn-outline btn-info btn-rounded" onclick="toUpdatequotaInfo()"
                   shiro:hasPermission="infoRecord:update">
                    <i class="fa fa-edit"></i> 修改价格/有效期
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="openFileChooser()"
                   shiro:hasPermission="infoRecord:batchImport">
                    <i class="fa fa-file-excel-o"></i> 批量导入
                </a>

            </div>
            <table id="inforecord"></table>
        </div>
    </div>
</div>

<form id="xlsForm" style="display:none" enctype="multipart/form-data">
    <input type="file" id="xlsFile" name="xlsFile">
</form>
<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--  -->
<script type="text/javascript">
    //面向对象
    var searcherBS = Searcher.createBS('#inforecord',
        [
            {checkbox: true},
            {
                title: '确认', field: 'infoStatus',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return "<img src='" + ctxPath + "/img/light/green.png' width='20px' />";
                    } else {
                        return "<img src='" + ctxPath + "/img/light/red.png' width='20px' />";
                    }
                }
            },
            {
                title: '冻结', field: 'freeze',
                formatter: function (value, row, index) {
                    if (value === 0) {
                        return "<img src='" + ctxPath + "/img/light/green.png' width='20px' />";
                    } else {
                        return "<img src='" + ctxPath + "/img/light/red.png' width='20px' />";
                    }
                }
            },
            {title: '信息记录编号', field: 'infoCode'},
            {title: '物料号', field: 'materialCode'},
            {
                title: '信息类别', field: 'classify',
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "寄售";
                    } else if (value == 0) {
                        return "标准";
                    } else if (value == "P") {
                        return "管道";
                    } else if (value == 3) {
                        return "外协加工";
                    }
                }
            },
            {title: '物料描述', field: 'materialDesc'},
            {title: '物料组', field: 'materialGroup'},
            {title: '供应商编码', field: 'srmCode'},
            {title: '供应商名称', field: 'vendorName'},
            /*{title: '工厂', field: 'werks'},*/
            {title: '采购组', field: 'purchaseGroup'},
            {title: '采购组织', field: 'purchArg'},
            {title: '税代码', field: 'taxCode'},
            {title: '价格', field: 'price'},
            {title: '币种', field: 'currency'},
            {title: '价格单位', field: 'priceUnit'},
            {title: '标准数量', field: 'standardNum'},
            {title: '周期', field: 'cycle'},
            {title: '条件记录有效起始日', field: 'startData'},
            {title: '价格有效日结束时间', field: 'priceEndData'},
            {title: '采购订单单位', field: 'purchaseUnit'}
        ]
    );
    $(function () {
        //初始化所有字典数据
        initDictDataSel();
        $("#xlsFile").change(function () {
            var filename = $(this).val();
            if (filename.lastIndexOf(".") >= 0x0) {
                filename = filename.substring(filename.lastIndexOf(".")).toLowerCase();
            } else {
                filename = "";
            }
            if (filename != ".xls" && filename != ".xlsx") {
                Dialog.errorMsg("只允许上传Excel文件！");
                return;
            }
            var formData = new FormData($('#xlsForm')[0]);
            Page.ajaxPOST(ctxPath + "/inforecord/imp", formData, function (o) {
                //dataGrid.getData({'url': ctxPath + '/vendor/datalList?theID='+o.theID});
                var index = Dialog.openUrl("导入数据", ctxPath + '/modules/inforecord/inforecord_imp_data.html', {
                    area: ['800px', '700px'],
                    maxmin: true,
                    end: function () {
                    }
                });
            }, {cache: false, processData: false, async: false, contentType: false, loadingMsg: "正在往服务器上传数据……"});
            $(this).val("");
        });
    });

    /**
     * 查询数据
     */
    function refresh() {
        searcherBS.refresh();
    }

    /**
     * @Description: 编辑更新信息记录方法
     */
    function toEditquotaInfo() {
        //获取id
        var pkId = searcherBS.getSelectedRows("pkId");
        if (pkId.length != 1) {
            Dialog.errorMsg("请选择一行进行编辑");
            return;
        }
        var status = searcherBS.getSelectedRows("infoStatus");
        if (status == 1) {
            Dialog.errorMsg("该记录已确认过,不可编辑！");
            return;
        }
        var index = Dialog.openUrl("信息记录更改", ctxPath + '/inforecord/toEdit/' + pkId, {
            area: ['900px', '700px'],
            maxmin: true,
            end: function () {
            }
        });
        layer.full(index);
    }

    /**
     * 确认采购员编辑的信息记录，确认后，后台同步sap
     */
    function confirmInfo() {
        //获取id
        var pkId = searcherBS.getSelectedRows("pkId");
        if (pkId.length != 1) {
            Dialog.errorMsg("请选择要确认的记录！");
            return false;
        }
        var status = searcherBS.getSelectedRows("infoStatus");
        var rawData = searcherBS.getRawData();
        for (let i = 0; i < status.length; i++) {
            if (status[i] == 1) {
                Dialog.errorMsg("物料 " + rawData[i].materialCode + " 已确认过！");
                return false;
            }
            if (rawData[i].startData.length == 0 || rawData[i].cycle.length == 0 || rawData[i].cycle == 0) {
                Dialog.errorMsg("请填写物料 " + rawData[i].materialCode + " 的周期、标准数量，确认价格、价格单位！", null, {time: 4000});
                return false;
            }
        }
        $.ajax({
            url: ctxPath + '/inforecord/confirmInfo/' + pkId,
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    Dialog.msg(data.msg);
                    refresh();
                } else {
                    Dialog.errorMsg(data.msg, null, {time: 3000});
                }
            },
            error: function (data) {

            }
        });
    }

    /**
     * 跳转至新增页
     */
    function toAddinfo() {
        var index = Dialog.openUrl("新增信息记录", ctxPath + '/modules/inforecord/inforecod_add.html',
            {
                area: ['900px', '700px'],
                maxmin: true,
                end: function () {
                }
            });
        layer.full(index);
    }


    /**
     * 冻结或者解冻
     */
    function freezeOrThaw() {
        var rowDatas = searcherBS.getSelectedRows("infoStatus");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行解冻或者冻结");
            return;
        }
        if (rowDatas == 0) {
            Dialog.errorMsg("未确认的信息记录不可冻结");
            return;
        }
        var rowData = searcherBS.getSelectedRows("pkId");
        layer.load(1, {shade: [0.3, '#393D49']});
        $.ajax({
            url: ctxPath + "/inforecord/freezeorthraw/" + rowData,
            type: 'POST',
            dataType: "json",
            cache: false, // 不缓存
            success: function (data) {
                layer.closeAll();
                if (data.code == 500) {
                    Dialog.errorMsg("操作失败！");
                } else {
                    Dialog.msg("操作成功!");
                }
                searcherBS.refresh();
            },
            error: function (data) {
                layer.closeAll();
                Dialog.errorMsg("操作失败！");
            }
        });
    }

    /**
     *修改
     */
    function toUpdatequotaInfo() {
        var rowDatas = searcherBS.getSelectedRows("infoStatus");
        var rowData1 = searcherBS.getSelectedRows("freeze");
        var infoCode = searcherBS.getSelectedRows("infoCode");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行修改！");
            return;
        }
        if (rowDatas == 0) {
            Dialog.errorMsg("未确认的信息记录不可以修改！");
            return;
        }
        if (rowData1 == 1) {
            Dialog.errorMsg("已冻结的信息记录不可以修改！");
            return;
        }
        if (infoCode == null || infoCode == "" || infoCode == undefined) {
            Dialog.errorMsg("点价的信息记录不可以修改！");
            return;
        }
        var pkId = searcherBS.getSelectedRows("pkId");
        var index = Dialog.openUrl("信息记录更改", ctxPath + '/inforecord/toUpdate/' + pkId, {
            area: ['900px', '700px'],
            maxmin: true,
            end: function () {
            }
        });
        layer.full(index);
    }

    /**
     * 点击“批量导入”按钮，打开文件选择框
     **/
    function openFileChooser() {
        $("#xlsFile").click();
    }

    /**
     * 取消确认
     */
    function unconfirmInfo() {
        var rowData = searcherBS.getSelectedRows("infoStatus");
        var rowDatas = searcherBS.getSelectedRows("pkId");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行取消！");
            return;
        }
        if (rowData[0] = 0) {
            Dialog.errorMsg("改信息记录还没有确认！");
            return;
        }
        $.ajax({
            url: ctxPath + "/inforecord/unconfirmInfo/" + rowDatas,
            type: 'POST',
            dataType: "json",
            cache: false, // 不缓存
            success: function (data) {
                layer.closeAll();
                if (data.code == 500) {
                    Dialog.errorMsg("操作失败！");
                } else {
                    Dialog.msg("操作成功!");
                }
                searcherBS.refresh();
            },
            error: function (data) {
                layer.closeAll();
                Dialog.errorMsg("操作失败！");
            }
        });
    }

</script>
</body>
